package com.geek.framework.study;

import org.junit.Test;

import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.HashMap;

public class ReflectiveDemo {


    static class StringList extends ArrayList<String> {
    }

    /**
     * 目标:获取StringList的泛型 : String
     */
    @Test
    public void test() {
        Type type = StringList.class.getGenericSuperclass();
        System.out.println("type = " + type);
        System.out.println("type.getClass() = " + type.getClass());
        if (type instanceof ParameterizedType) {
            ParameterizedType parameterizedType = (ParameterizedType) type;
            System.out.println("parameterizedType.getRawType() = " + parameterizedType.getRawType());
            System.out.println("parameterizedType.getOwnerType() = " + parameterizedType.getOwnerType());
            for (Type actualTypeArgument : parameterizedType.getActualTypeArguments()) {
                System.out.println("actualTypeArgument.getTypeName() = " + actualTypeArgument.getTypeName());
            }
        }
        for (Type anInterface : HashMap.class.getGenericInterfaces()) {
            System.out.println("anInterface = " + anInterface);
        }
        System.out.println("HashMap.class.getSuperclass() = " + HashMap.class.getSuperclass());
        System.out.println("String.class.getGenericSuperclass() = " + String.class.getGenericSuperclass());
    }
}
